<?php
/**
 * @return Logger
 */
function logger(){
    return Logger::getInstance();
}

/**
 * Logging Class
 *
 */
class Logger {  
    //flag of if debug, 决定是否要同时打印到屏幕上 should be off when online
    static $currrentMsg;
    var $screenFlag=false;
    var $log_path = G_LOG_PATH;
    var $_threshold = 2;
    var $_date_fmt  = 'Y-m-d H:i:s';
    var $_enabled   = TRUE;
    var $_levels    = array('ERROR' => 1,   'INFO' => 2, 'DEBUG' => 3,'ALL' => 4);
    public $extraInfo=false;

    /**
     * Constructor 
     * 
     * @access  public 
     */  
    private function __construct()
    {

        if ( ! is_dir($this->log_path))  
        {  
            if($this->screenFlag) echo("Error! log file folder not exist!".$this->log_path."\n");
            $this->_enabled = FALSE;  
        }  
              
        if (G_LOG_DATE_FORMAT)
        {  
            $this->_date_fmt = G_LOG_DATE_FORMAT;
        }  
        if (G_DEBUG_ON)
            $this->_threshold = 3;
        date_default_timezone_set('Asia/Chongqing');
    }  
    public static function getInstance(){
        static $instance;
        if(empty($instance)) $instance=new Logger();
        return $instance;
    }

    public static function getInstance2() {
        static $instance2;
        if(empty($instance2)) $instance2 = new Logger();
        return $instance2;
    }
    public function setScreenFlag($enable){
        $this->screenFlag=$enable;
    }
    
     function logInfo($msg){
        return $this->write_log($msg,'INFO');
     }

    /**
     * @param string $msg
     * @param string $level the error level
     * @param bool $php_error whether the error is a native PHP error
     * @return bool
     */
    private function write_log( $msg,$level = 'ERROR', $php_error = FALSE)
    {
        if ($this->_enabled === FALSE)
        {
            echo ("Log class not initated!!!");
            return FALSE;
        }

        $level = strtoupper($level);

        if ( ! isset($this->_levels[$level]) OR ($this->_levels[$level] > $this->_threshold))
        {
            return FALSE;
        }

        $filePath = $this->log_path.DIRECTORY_SEPARATOR.'log-'.date('Y-m-d').'.log';
        $message  = '';

        if ( ! $fp = @fopen($filePath, 'a'))
       // if ( ! $fp = @fopen($filepath, FOPEN_READ_WRITE_CREATE))
        {
            if($this->screenFlag) echo("log file open error! ".$filePath."\n");
            return FALSE;
        }

        $message .= $level.' '.(($level == 'INFO') ? ' -' : '-').' '.date($this->_date_fmt). ': '.$msg."\r\n";

        flock($fp, LOCK_EX);
        fwrite($fp, $message);
        flock($fp, LOCK_UN);
        fclose($fp);


        if($this->screenFlag) echo($message."\n");
        return TRUE;
    }

     function logError($msg){
         return $this->write_log($msg,'ERROR');
     }

     function logDebug($msg){
         return $this->write_log($msg,'DEBUG');
     }

     function logExctra($msg,$hint=''){
         if($this->extraInfo=true) return $this->write_log($hint.':'.var_export($msg,true),'DEBUG');
         else return false;
     }

    /**
     *
     * @param object $msg
     * @param string  $explain 对当前信息的说明
     * @return boolean
     */
     function logDebug2($msg,$explain=''){
         return $this->write_log("\n<br>--".$explain." ".$this->get_variable_name($msg).':'.var_export($msg,true).'','DEBUG');
     }

    //获得变量的名称：）

    function get_variable_name(&$var, $scope=null){
    
        $scope = $scope==null? $GLOBALS : $scope; // 如果没有范围则在globals中找寻
    
        // 因有可能有相同值的变量,因此先将当前变量的值保存到一个临时变量中,然后再对原变量赋唯一值,以便查找出变量的名称,找到名字后,将临时变量的值重新赋值到原变量
        $tmp = $var;
    
        $var = 'tmp_value_'.mt_rand();
        $name = array_search($var, $scope, true); // 根据值查找变量名称
    
        $var = $tmp;
        return $name;
    }
    function IsValidString($C_char){
        if (!is_string($C_char)) return false; //判断是否是字符串类型
        if (empty($C_char)) return false; //判断是否已定义字符串
        if ($C_char=='') return false; //判断字符串是否为空
        return true;
    }
}
    // END Log Class
    
/* End of file Log.php */

